package com.moss.cloud.plugins.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.common.base.utils.PageUtil;
import com.moss.cloud.common.core.exception.SystemErrorType;
import com.moss.cloud.common.core.utils.BooleanHandel;
import com.moss.cloud.plugins.api.model.from.BaseFrom;
import com.moss.cloud.plugins.api.model.from.DataSourceConfFrom;
import com.moss.cloud.plugins.api.model.param.DataSourceConfParam;
import com.moss.cloud.plugins.biz.mapper.DataSourceConfMapper;
import com.moss.cloud.plugins.biz.model.DataSourceConf;
import com.moss.cloud.plugins.biz.service.IDataSourceConfService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 数据源配置业务实现
 *
 * @author 瑾年
 * @date 2024年1月3日
 */
@Slf4j
@Service
public class DataSourceConfServiceImpl extends ServiceImpl<DataSourceConfMapper, DataSourceConf> implements IDataSourceConfService {
    /**
     * 新增数据源配置
     *
     * @param dataSourceConfFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDataSourceConf(DataSourceConfFrom dataSourceConfFrom) {
        this.checkDataSource(dataSourceConfFrom, Boolean.TRUE);
        DataSourceConf dataSourceConf = new DataSourceConf();
        BeanUtils.copyProperties(dataSourceConfFrom, dataSourceConf);
        return this.save(dataSourceConf);
    }

    /**
     * 更新数据源配置
     *
     * @param dataSourceConfFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDataSourceConf(DataSourceConfFrom dataSourceConfFrom) {
        BooleanHandel.trueThrow(Objects.isNull(dataSourceConfFrom.getId())).throwMessage(SystemErrorType.PRIMARY_KEY);
        DataSourceConf conf = this.getById(dataSourceConfFrom.getId());
        BooleanHandel.trueThrow(Objects.isNull(conf)).throwMessage(SystemErrorType.DATE_EMPTY);
        this.checkDataSource(dataSourceConfFrom, Boolean.FALSE);
        DataSourceConf dataSourceConf = new DataSourceConf();
        BeanUtils.copyProperties(dataSourceConfFrom, dataSourceConf);
        return this.updateById(dataSourceConf);
    }

    /**
     * 验证数据唯一性
     *
     * @param dataSourceConfFrom
     * @param isAdd
     */
    private void checkDataSource(DataSourceConfFrom dataSourceConfFrom, boolean isAdd) {
        List<DataSourceConf> confs = this.list(new QueryWrapper<DataSourceConf>().lambda().eq(DataSourceConf::getDatabaseName, dataSourceConfFrom.getDatabaseName()));
        BooleanHandel.trueThrow(isAdd && CollectionUtils.isNotEmpty(confs)).throwMessage(SystemErrorType.DATA_SOURCE_EXIT);
        BooleanHandel.isTrue(!isAdd).trueHandle(() -> {
            confs.removeIf(item -> item.getId().equals(dataSourceConfFrom.getId()));
            BooleanHandel.trueThrow(CollectionUtils.isNotEmpty(confs)).throwMessage(SystemErrorType.DATA_SOURCE_EXIT);
        });
    }

    /**
     * 删除数据源配置
     *
     * @param baseFrom
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeDataSourceConf(BaseFrom baseFrom) {
        DataSourceConf conf = this.getById(baseFrom.getId());
        BooleanHandel.trueThrow(Objects.isNull(conf)).throwMessage(SystemErrorType.DATE_EMPTY);
        return this.removeById(baseFrom.getId());
    }

    /**
     * 数据源配置分页查询
     *
     * @param dataSourceConfParam
     * @return
     */
    @Override
    public IPage<DataSourceConf> page(DataSourceConfParam dataSourceConfParam) {
        Page<DataSourceConf> page = PageUtil.createPage(dataSourceConfParam.getCurrent(), dataSourceConfParam.getSize());
        return this.page(page, new QueryWrapper<DataSourceConf>().lambda()
                .eq(StringUtils.isNotBlank(dataSourceConfParam.getDataSourceType()), DataSourceConf::getDataSourceType, dataSourceConfParam.getDataSourceType())
                .like(StringUtils.isNotBlank(dataSourceConfParam.getDatabaseHost()), DataSourceConf::getDatabaseHost, dataSourceConfParam.getDatabaseHost()));
    }
}
